package com.jinzhuo.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.jinzhuo.music.domain.Comment;
import com.jinzhuo.music.domain.Singer;
import com.jinzhuo.music.service.CommentService;
import com.jinzhuo.music.service.SingerService;
import com.jinzhuo.music.utils.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 添加评价
     */
    @RequestMapping(value = "addComment",method = RequestMethod.POST)
    public Object addComment(HttpServletRequest request){
        JSONObject object = new JSONObject();
        String userId = request.getParameter("userId");
        String type = request.getParameter("type");
        String songId = request.getParameter("songId");
        String songListId = request.getParameter("songListId");
        String content = request.getParameter("content");
//        String up = request.getParameter("up");

        //保存到评价的对象中
        Comment comment = new Comment();
        comment.setUserId(Integer.parseInt(userId));
//        comment.setType(new Byte(type));
        if (new Byte(type)==0){
            comment.setSongId(Integer.parseInt(songId));
        }else {
            comment.setSongListId(Integer.parseInt(songListId));
        }
        comment.setContent(content);
        comment.setType(new Byte(type));
//        comment.setUp(Integer.parseInt(up));
        boolean flag = commentService.addComment(comment);
        if (flag){
            object.put(Consts.CODE,"1");
            object.put(Consts.MSG,"评价成功");
        }else {
            object.put(Consts.CODE,0);
            object.put(Consts.MSG,"评价失败");
        }
        return object;
    }

    /**
     * 修改评价
     */
    @RequestMapping(value = "updateComment",method = RequestMethod.POST)
    public Object updateComment(HttpServletRequest request){
        JSONObject object = new JSONObject();
        String id = request.getParameter("id").trim();
        String userId = request.getParameter("userId").trim();
        String type = request.getParameter("type").trim();
        String songId = request.getParameter("songId").trim();
        String songListId = request.getParameter("songListId").trim();
        String content = request.getParameter("content").trim();
//        String up = request.getParameter("up").trim();

        //保存到歌手的对象中
        Comment comment = new Comment();
        comment.setId(Integer.parseInt(id));
        comment.setUserId(Integer.parseInt(userId));
        comment.setType(new Byte(type));
        if (songId!=null&&songId.equals("")){
            songId=null;
        }else {
            comment.setSongId(Integer.parseInt(songId));
        }
        if (songListId!=null&&songListId.equals("")){
            songListId=null;
        }else {
            comment.setSongListId(Integer.parseInt(songListId));
        }
        comment.setContent(content);
        boolean flag = commentService.updateComment(comment);
        if (flag){
            object.put(Consts.CODE,"1");
            object.put(Consts.MSG,"修改成功");
        }else {
            object.put(Consts.CODE,0);
            object.put(Consts.MSG,"修改失败");
        }
        return object;
    }

    /**
     * 删除评价（根据歌单id去删除)
     */
    @RequestMapping(value = "deleteComment",method = RequestMethod.POST)
    public Object deleteComment(HttpServletRequest request) {
        JSONObject object = new JSONObject();
        String id = request.getParameter("id");
        boolean flag = commentService.deleteComment(Integer.parseInt(id));
        return flag;
    }

    /**
     * 据主键查询单个对象
     */
    @RequestMapping(value = "queryComment",method = RequestMethod.POST)
    public Object queryComment(HttpServletRequest request) {
        String id = request.getParameter("id").trim();
        Comment comment = commentService.queryComment(Integer.parseInt(id));
        return comment;
    }

    /**
     * 查询所有对象
     */
    @RequestMapping(value = "queryAllComment",method = RequestMethod.POST)
    public Object queryAllComment(HttpServletRequest request) {
//        String id = request.getParameter("id").trim();
        return commentService.queryAllComment();
    }

    /**
     * 查询某个歌曲下的评论
     */
    @RequestMapping(value = "queryBySongId",method = RequestMethod.POST)
    public Object queryBySongId(HttpServletRequest request) {
        String songId =request.getParameter("songId").trim();
        List<Comment> comments = commentService.queryBySongId(Integer.parseInt(songId));
        return comments;
    }

    /**
     * 查询某个歌单下的所有评论
     */
    @RequestMapping(value = "queryBySongListId",method = RequestMethod.POST)
    public Object queryBySongListId(HttpServletRequest request) {
        String songListId =request.getParameter("songListId");
        List<Comment> comments = commentService.queryBySongListId(Integer.parseInt(songListId));
        return comments;
    }

    /**
     * 点赞功能
     */
    @RequestMapping(value = "like",method = RequestMethod.POST)
    public Object like(HttpServletRequest request){
        JSONObject object = new JSONObject();
        String up = request.getParameter("up");
        String id = request.getParameter("id");

        Comment comment = new Comment();
        comment.setId(Integer.parseInt(id));
        comment.setUp(Integer.parseInt(up));
        boolean flag = commentService.updateComment(comment);
        if (flag){
             object.put(Consts.CODE,"1");
             object.put(Consts.MSG,"点赞成功");
        }else {
             object.put(Consts.CODE,"0");
             object.put(Consts.MSG,"点赞失败");
        }
        return object;
    }


}
